<?php

namespace App\Admin\Controllers;
use Illuminate\Http\Request;
use App\supplier_type;
use App\crm_supplier;
use Validator;
use Illuminate\Support\MessageBag;
// use Encore\Admin\Form;
use Encore\Admin\Widgets\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Supplier_typeController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->grid());
        });
    }

    public function show($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('供应商账户');
            $content->description('列表');

            $content->body($this->grid($id));
        });
    }
     public function store(Request $request)
    {
        //前端验证不得为空
        $list=$request->except("_token");
        $rule=[
            'typename'=>'required',
            'name'=>'required',
            'zhanghao'=>'required',
        ];
        $mess=[
            'typename.required'=>'账户类型必须输入',
            'zhanghao.required'=>'账户号必须输入',
            'name.required'=>'账户名称必须输入',
        ];
        $validator =  Validator::make($list,$rule,$mess);
        if($validator->passes()){
            //执行添加
            $res=supplier_type::create($list);
            if($res){
                $success = new MessageBag([
                    'title'   => '添加成功',
                ]);
                return redirect(admin_url('supplier_type/'.$list['supplier_id']))->with(compact('success'));
            }else{
                $error = new MessageBag([
                    'title'   => '添加失败',
                ]);
                return back()->with(compact('error'));    
            }
        }else{
            return back()->withErrors($validator);
        }
    }
    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function tianjia($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('账户添加');
            $content->description('添加');
            $form=new Form();
            $form->action(admin_url('supplier_type'));
            $form->text("typename",'账户类型');
            $form->text("name",'账户名称');
            $form->text("zhanghao",'账户号');
            $form->text("kaihu",'开户行');
            $form->hidden('supplier_id','供应商Id')->default($id);
            $content->body($form);
        });
    }
    /**
     * Create interface.
     *
     * @return Content
     */
    // 执行修改
    public function xiugai(Request $request, $id)
    {
         $list=$request->except("_token","_method");
         $reab=supplier_type::find($id);
         $reab->typename=$list['typename'];
         $reab->zhanghao=$list['zhanghao'];
         $reab->supplier_id=$list['supplier_id'];
         $res=$reab->save();
         if($res){
            $success = new MessageBag([
                    'title'   => '修改成功',
                ]);
            return redirect(admin_url('supplier_type/'.$list['supplier_id']))->with(compact('success'));
         }else{
            $error = new MessageBag([
                    'title'   => '修改失败',
                ]);
            return back()->with(compact('error'));    
         }
    }
    // 调用编辑页面
    public function info($id)
    {
        return Admin::content(function (Content $content) use ($id) {
            $content->header('供应商账户');
            $content->description('修改');
            // new 一个form表单
            $list=supplier_type::find($id);
            $form = new Form();
            $form->action(admin_url('supplier_type/update/'.$id));
            $form->disablePjax();
            $form->text('typename','账户类型')->default($list->typename)->rules('required');
            $form->text('zhname','账户名称')->default($list->typename)->rules('required');
            $form->text('zhanghao','账户号')->default($list->zhanghao)->rules('required');
            $form->text('kaihu','开户行')->default($list->zhanghao)->rules('required');
            $form->hidden('supplier_id','供应商id')->default($id);
            $content->body($form);
        });
    }
    // 删除
    public function del($id)
    {
        $res=supplier_type::find($id)->delete();
        if($res){
            $success = new MessageBag([
                    'title'   => '删除成功',
                ]);
            return back()->with(compact('success'));
        }else{
            $error = new MessageBag([
                    'title'   => '删除失败',
                ]);
            return back()->with(compact('error'));
        }
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid($id)
    {
        return Admin::grid(supplier_type::class, function (Grid $grid) use ($id) {
            $grid->model()->where('supplier_id', $id);
            $grid->id('序号')->sortable();
            // $grid->supplier_id('公司名称');
            // $grid->crm_supplier()->supplier_name('公司名称');
            $grid->typename('账户类型');
            $grid->name('账户名称');
            $grid->zhanghao('账户号');
            $grid->kaihu('开户行');
            $grid->disableFilter(); //禁用筛选
            $grid->disableExport(); //禁止导出
            $grid->disableRowSelector(); //禁止行选择
            $grid->actions(function ($actions) {
                $actions->disableDelete();
                $actions->disableEdit();
                $actions->append("<a href='".admin_url('supplier_type/info/'.$actions->getKey())."'><i class='fa fa-edit'></i></a>");
                $actions->append("<a href='".admin_url('supplier_type/del/'.$actions->getKey())."'><i class='fa fa-trash'></i></a>");
            });
            $script_addones=<<<EOT
                $(".fa-trash").click(function(){
                    if(confirm("是否确认删除")){
                        return true;
                    }else{
                        return false;
                    }
                })
EOT;
            Admin::script($script_addones);
        });
    }

}
